package mapEditor.view.resItemEdit
{
	import asset.mapEditorItemAssetListItemViewAsset;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mapEditor.data.AssetTemplateInfo;
	import mapEditor.vo.AssetVO;
	import mapEditor.vo.ResItemItemVO;
	
	import utils.ApplicationDomainUtil;
	import utils.DisplayUtil;
	
	public class ItemPropertyAssetItemView extends mapEditorItemAssetListItemViewAsset
	{
		private var _resItemItemVO:ResItemItemVO;
		private var _assetObject:DisplayObject;
		private var _assetVO:AssetVO;
		
		public function ItemPropertyAssetItemView(resItemItemVO:ResItemItemVO)
		{
			_resItemItemVO=resItemItemVO;
			_resItemItemVO.addEventListener(Event.CHANGE, __onResItemItemVOChange);
			setInfo();
			gotoAndStop(1);
			chkIsVisible.gotoAndStop(1);
			
			chkIsVisible.addEventListener(MouseEvent.CLICK, __onChkIsVisibleClick);
			addEventListener(MouseEvent.CLICK, __onMouseEvent);
			addEventListener(MouseEvent.MOUSE_OVER, __onMouseEvent);
			addEventListener(MouseEvent.MOUSE_OUT, __onMouseEvent);
		}
		
		private function __onResItemItemVOChange(evt:Event):void
		{
			gotoAndStop(_resItemItemVO.selected ? 2 : 1);
			chkIsVisible.gotoAndStop(_resItemItemVO.visible ? 1 : 2);
		}
		
		private function __onMouseEvent(evt:MouseEvent):void
		{
			switch(evt.type)
			{
				case MouseEvent.CLICK:
					_resItemItemVO.selected=!_resItemItemVO.selected;
					_resItemItemVO.change();
					dispatchEvent(new Event(Event.SELECT));
					break;
				case MouseEvent.MOUSE_OVER:
					gotoAndStop(2);
					break;
				case MouseEvent.MOUSE_OUT:
					if(!_resItemItemVO.selected)gotoAndStop(1);
					break;
			}
		}
		
		private function __onChkIsVisibleClick(evt:MouseEvent):void
		{
			_resItemItemVO.visible=!_resItemItemVO.visible;
			chkIsVisible.gotoAndStop(_resItemItemVO.visible ? 1 : 2);
			_resItemItemVO.change();
			evt.stopImmediatePropagation();
		}
		
		private function setInfo():void
		{
			_assetVO=AssetTemplateInfo.instance.getAssetVOByID(_resItemItemVO.assetID);
			var obj:*=ApplicationDomainUtil.getDefinitionObject(_assetVO.className);
			if(obj is BitmapData)
			{
				_assetObject=new Bitmap(obj);
			}
			else if(obj is MovieClip)
			{
				_assetObject=obj as DisplayObject;
			}
			if(_assetObject.width>30)_assetObject.width=30;
			if(_assetObject.height>30)_assetObject.height=30;
			_assetObject.x=(30-_assetObject.width)/2;
			_assetObject.y=(30-_assetObject.height)/2;
			boxPreview.addChild(_assetObject);
			
			txtName.text=_assetVO.name;
			txtID.text=_resItemItemVO.assetID.toString();
		}
		
		public function dispose():void
		{
			chkIsVisible.removeEventListener(MouseEvent.CLICK, __onChkIsVisibleClick);
			removeEventListener(MouseEvent.CLICK, __onMouseEvent);
			removeEventListener(MouseEvent.MOUSE_OVER, __onMouseEvent);
			removeEventListener(MouseEvent.MOUSE_OUT, __onMouseEvent);
			
			if(_resItemItemVO)
			{
				_resItemItemVO.removeEventListener(Event.CHANGE, __onResItemItemVOChange);
				_resItemItemVO.dispose();
				_resItemItemVO=null;	
			}
			DisplayUtil.dispose(_assetObject);
			_assetObject=null;
			_assetVO=null;			
			if(parent)parent.removeChild(this);
		}

		public function get resItemItemVO():ResItemItemVO
		{
			return _resItemItemVO;
		}
	}
}